home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / admin / xinetd.2 / xinetd / xinetd.2.1.7-linux.4 / libs / src / sio / sio.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-25  |  6.5 KB  |  257 lines

  1. /*
  2.  * (c) Copyright 1992, 1993 by Panagiotis Tsirigotis
  3.  * All rights reserved.  The file named COPYRIGHT specifies the terms 
  4.  * and conditions for redistribution.
  5.  */
  6.  
  7. /*
  8.  * $Id: sio.h,v 8.1 1993/03/13 01:13:58 panos Exp $
  9.  */
  10.  
  11. #ifndef __SIO_H
  12. #define __SIO_H
  13.  
  14. #include <errno.h>
  15. #include <varargs.h>
  16.  
  17. /*
  18.  * Naming conventions:
  19.  *        1) SIO functions and macros have names starting with a capital S
  20.  *        2) SIO constants meant to be used by user programs have 
  21.  *            names starting with SIO_
  22.  *        3) Internal functions, struct identifiers, enum identifiers 
  23.  *            etc. have names starting with __sio
  24.  *        4) Internal constants and macros have names starting with __SIO
  25.  */
  26.  
  27.  
  28. /*
  29.  * external constants
  30.  *
  31.  * SIO_FLUSH_ALL: flush all output streams
  32.  * SIO_EOF:    eof on stream
  33.  * SIO_ERR: operation failed
  34.  */
  35. #define SIO_FLUSH_ALL                (-1)
  36. #define SIO_EOF                        (-2)
  37. #define SIO_ERR                        (-1)
  38.  
  39. /*
  40.  * Undo types
  41.  */
  42. #define SIO_UNDO_LINE        0
  43. #define SIO_UNDO_CHAR        1
  44.  
  45. /*
  46.  * Buffering types
  47.  */
  48. #define SIO_FULLBUF            0
  49. #define SIO_LINEBUF            1
  50. #define SIO_NOBUF                2
  51.  
  52. /*
  53.  * Descriptor for an input stream
  54.  */
  55. struct __sio_input_descriptor
  56. {
  57.     /*
  58.      * buf:        points to the buffer area.
  59.      *                When doing memory mapping, it is equal to the unit 
  60.      *                from which we are reading. When doing buffered I/O
  61.      *                it points to the primary buffer.
  62.      */
  63.     char *buf ;
  64.     unsigned buffer_size ;
  65.  
  66.     char *start ;                 /* start of valid buffer contents       */
  67.     char *end ;                   /* end of valid buffer contents + 1     */
  68.     char *nextb ;                 /* pointer to next byte to read/write     */
  69.                                             /* Always:  start <= nextb < end            */
  70.  
  71.     unsigned line_length ;
  72.     int max_line_length ;
  73.     int tied_fd ;
  74.  
  75.     int memory_mapped ;                /* flag to denote if we use                */
  76.                                             /* memory mapping                                */
  77. } ;
  78.  
  79. typedef struct __sio_input_descriptor __sio_id_t ;
  80.  
  81.  
  82. /*
  83.  * Descriptor for an output stream
  84.  */
  85. struct __sio_output_descriptor
  86. {
  87.     /*
  88.      * buf:        points to the buffer area.
  89.      * buf_end: is equal to buf + buffer_size
  90.      */
  91.     char *buf ;
  92.     char *buf_end ;
  93.  
  94.     unsigned buffer_size ;
  95.  
  96.     char *start ;                 /* start of valid buffer contents       */
  97.                                             /* (used by the R and W functions)         */
  98.     char *nextb ;                 /* pointer to next byte to read/write  */
  99.                                             /* Always:  start <= nextb < buf_end    */
  100.     int buftype ;                        /* type of buffering                         */
  101. } ;
  102.  
  103. typedef struct __sio_output_descriptor __sio_od_t ;
  104.  
  105.  
  106.  
  107. /*
  108.  * Stream types
  109.  */
  110. enum __sio_stream { __SIO_INPUT_STREAM, __SIO_OUTPUT_STREAM } ;
  111.  
  112.  
  113. /*
  114.  * General descriptor
  115.  */
  116. struct __sio_descriptor
  117. {
  118.     union
  119.     {
  120.         __sio_id_t input_descriptor ;
  121.         __sio_od_t output_descriptor ;
  122.     } descriptor ;
  123.     enum __sio_stream stream_type ;
  124.     int initialized ;
  125. } ;
  126.  
  127. typedef struct __sio_descriptor __sio_descriptor_t ;
  128.  
  129.  
  130. /*
  131.  * The array of descriptors (as many as available file descriptors)
  132.  */
  133. extern __sio_descriptor_t *__sio_descriptors ;
  134.  
  135. extern int errno ;
  136.  
  137.  
  138. /*
  139.  * Internally used macros
  140.  */
  141. #define __SIO_FD_INITIALIZED( fd )        (__sio_descriptors[ fd ].initialized)
  142. #define __SIO_ID( fd )    (__sio_descriptors[ fd ].descriptor.input_descriptor)
  143. #define __SIO_OD( fd )    (__sio_descriptors[ fd ].descriptor.output_descriptor)
  144. #define __SIO_MUST_FLUSH( od, ch )                                                    \
  145.                     ( (od).buftype != SIO_FULLBUF &&                                    \
  146.                         ( (od).buftype == SIO_NOBUF || ch == '\n' ) )
  147.  
  148.  
  149. /*
  150.  * SIO Macros:
  151.  *
  152.  *        SIOLINELEN( fd )
  153.  *        SIOMAXLINELEN( fd )
  154.  *        Sputchar( fd, c )
  155.  *        Sgetchar( fd )
  156.  *
  157.  * NOTE: The maximum line size depends on whether the descriptor
  158.  *            was originally memory mapped. If it was, then the maximum
  159.  *            line size will be the map_unit_size (a function of the system
  160.  *            page size and PAGES_MAPPED). Otherwise, it will be either the
  161.  *            optimal block size as reported by stat(2) or SIO_BUFFER_SIZE.
  162.  */
  163.  
  164. #define SIOLINELEN( fd )      __SIO_ID( fd ).line_length
  165. #define SIOMAXLINELEN( fd )                                                                    \
  166.     (                                                                                                    \
  167.         __SIO_FD_INITIALIZED( fd )                                                                \
  168.             ? (                                                                                         \
  169.                  (__sio_descriptors[ fd ].stream_type == __SIO_INPUT_STREAM)        \
  170.                     ? __SIO_ID( fd ).max_line_length                                            \
  171.                     : ( errno = EBADF, SIO_ERR )                                                \
  172.               )                                                                                        \
  173.             : (        /* not initialized; initialize it for input */                    \
  174.                  (__sio_init( &__sio_descriptors[ fd ], fd, __SIO_INPUT_STREAM )    \
  175.                                                                                     == SIO_ERR)        \
  176.                     ? SIO_ERR                                                                        \
  177.                     : __SIO_ID( fd ).max_line_length                                            \
  178.               )                                                                                        \
  179.     )
  180.  
  181.  
  182.  
  183. /*
  184.  * Adds a character to a buffer, returns the character or SIO_ERR
  185.  */
  186. #define  __SIO_ADDCHAR( od, fd, c )                                  \
  187.      ( od.buftype == SIO_FULLBUF )                                   \
  188.          ? (int) ( *(od.nextb)++ = (unsigned char) (c) )             \
  189.          : ( od.buftype == SIO_LINEBUF )                             \
  190.                ? ( ( *(od.nextb) = (unsigned char) (c) ) != '\n' )   \
  191.                      ? (int) *(od.nextb)++                           \
  192.                      : Sputc( fd, *(od.nextb) )                      \
  193.                : Sputc( fd, c )
  194.  
  195.  
  196. /*
  197.  * The Sgetchar/Sputchar macros depend on the fact that the fields 
  198.  *                 nextb, buf_end, end
  199.  * are 0 if a stream descriptor is not being used or has not yet been
  200.  * initialized.
  201.  * This is true initially because of the static allocation of the
  202.  * descriptor array, and Sdone must make sure that it is true
  203.  * after I/O on a descriptor is over.
  204.  */
  205. #define Sputchar( fd, c )                                                        \
  206.         (                                                                                \
  207.             ( __SIO_OD( fd ).nextb < __SIO_OD( fd ).buf_end )            \
  208.                 ? ( __SIO_ADDCHAR( __SIO_OD( fd ), fd, c ) )                \
  209.                 : Sputc( fd, c )                                                    \
  210.         )
  211.  
  212. #define Sgetchar( fd )                                                    \
  213.         (                                                                        \
  214.             ( __SIO_ID( fd ).nextb < __SIO_ID( fd ).end )        \
  215.                 ? (int) *__SIO_ID( fd ).nextb++                         \
  216.                 : Sgetc( fd )                                                \
  217.         )
  218.  
  219.  
  220. #ifdef __ARGS
  221. #undef __ARGS
  222. #endif
  223.  
  224. #ifdef PROTOTYPES
  225. #    define __ARGS( s )                    s
  226. #else
  227. #    define __ARGS( s )                    ()
  228. #endif
  229.  
  230. /*
  231.  * The Read functions
  232.  */
  233. int Sread __ARGS( ( int fd, char *buf, int nbytes ) ) ;
  234. int Sgetc __ARGS( ( int fd ) ) ;
  235. char *Srdline __ARGS( ( int fd ) ) ;
  236. char *Sfetch __ARGS( ( int fd, long *length ) ) ;
  237.  
  238. /*
  239.  * The Write functions
  240.  */
  241. int Swrite __ARGS( ( int fd, char *buf, int nbytes ) ) ;
  242. int Sputc __ARGS( ( int fd, char c ) ) ;
  243. int Sprint __ARGS( ( int fd, char *format, ... ) ) ;
  244. int Sprintv __ARGS( ( int fd, char *format, va_list ) ) ;
  245.  
  246. /*
  247.  * other functions
  248.  */
  249. int Sdone __ARGS( ( int fd ) ) ;
  250. int Sundo __ARGS( ( int fd, int type ) ) ;
  251. int Sflush __ARGS( ( int fd ) ) ;
  252. int Sclose __ARGS( ( int fd ) ) ;
  253. int Sbuftype __ARGS( ( int fd, int type ) ) ;
  254.  
  255. #endif /* __SIO_H */
  256.  
  257.